package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.newEntity.WeixinPushModel;
import com.tbit.uqbike.client.service.WeixinPushModelService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序订阅消息推送
 *
 * @Author: chen
 * @Description: 小程序订阅消息推送
 * @Date: 创建于 9:19 2022/7/26
 */
@Controller
@RequestMapping("/weixinPush")
public class WeixinPushModelController {

    @Autowired
    private WeixinPushModelService weixinPushModelService;

    /**
     * 通过类型获取区域模板
     *
     * @param accountId 品牌id
     * @param type      类型
     * @param session
     * @param request
     * @param response
     */
    @RequestMapping("/getByType")
    public void getByType(Integer accountId, String type, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Assert.notNull(accountId, "-10001", "主键id不能为空");
            Assert.notNull(type, "-10001", "类型不能为空");
            String[] strings = type.split(",");
            List<Integer> list = new ArrayList<>();
            for (String s : strings) {
                list.add(Integer.valueOf(s));
            }
            List<WeixinPushModel> weixinPushModels = weixinPushModelService.getByType(accountId, list);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, weixinPushModels);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }
}
